<?php
require_once(LIB_PATH.'controller/root.con.php');
class userCon extends rootCon{

    /*
     * 从微信获取用户手机号码
     * */
    public function saveInfoAction(){
        $uid = mylogin::getUid(true);
        $user = new User();
        $user->find($uid);
        $user->load();

        if(r('avatar')){
            $file = "/up/avatar/".$uid.time().'.png';
            $content = myspider::fetch(r('avatar'));
            file_put_contents(ROOT_PATH.'web'.$file,$content);
            $user->avatar = $file;
        }
        $encryptedData = r('encryptedData');
        if($encryptedData){
            require_once(LIB_PATH."xiaochengxu/myxiaochengxu.php");
            $iv = r('iv');
            $user->mobile = myxiaochengxu::getMobile($encryptedData,$user->sessionKey,$iv);
        }

        $user->save();
        $this->j(0,$user->tidy());
    }

    /**
    * use code to get user's base information from weichat server
     */
    public function loginAction(){
        if(mylogin::isLogin()){
            $loginInfo['loginUid']   = r('loginUid');
            $loginInfo['loginTime']  = r('loginTime');
            $loginInfo['loginKey']   = r('loginKey');
            $this->j(0,'login info',$loginInfo);
        }

        require_once(LIB_PATH."xiaochengxu/myxiaochengxu.php");
        $json = myxiaochengxu::getBaseInfo(r('code'));

        $user = new User();
        //查找用户是否已经注册
        $find = $user->find('openId="'.$json->openid.'"');
        if(!$find){
        	//推荐人
            if(r_int('fromUid')) $user->fromUid = r_int('fromUid');
        }

        $user->openId = $json->openid;
        $user->unionId = $json->unionid;
        $user->sessionKey = $json->session_key;
        $user->ip = myrequest::getIP();
        
		//添加或更新
        $user->save(); //
        $uid = $user->uid;
        if(!$uid){
            $this->j(-1,'服务器居然出错了!',$json);
        }

        $rect = mylogin::login($uid,$json->session_key);
        $this->j(0,'login info',$rect);
    }

    /**
     * save user's information
     */
    public function saveAction() {
        $uid = mylogin::getUid(true);
        $user = new User();
        $find = $user->find(['uid',$uid]);
        if(!$find){
            $this->j(-1,'请重新登录');
        }
        $user->load();

        if($user->save()){
            $this->j(0, '成功保存信息');
        }else{
            $this->j(-1, '系统居然出错了。');
        }
    }

    /**
    * are you login?
     */
    public function isLoginAction() {
        $loginInfo = mylogin::getLoginInfo();
        if($loginInfo){
            j(array(
                'code' => 0,
                'uid'=>mylogin::getUid(),
                'data'=>$loginInfo
            ));
        }else{
            j(array(
                'code' => -1,
                'info'=>'notLogin'
            ));
        }
    }

    /**
    * change user's mobile
     */
    public function changeMobileAction(){
        $uid = mylogin::getUid(true);
        $oldMobileCode   = r('codeResult');
        $code   = r('code');
        $newMobile = r('newMobile');

        $user = new User();
        $user->find($uid);
        //check old mobile
        if(mylogin::getMd5Pwd($user->mobile)!=$oldMobileCode){
            $this->j(-4,'您需要先验证旧的手机号，才能修改手机号');
        }


        //check new mobile code
        $auth = new AuthcodeExt();
        $checkResult = $auth->check($newMobile, $code);
        if($checkResult[0]!=0){
            $this->j( $checkResult[0], $checkResult[1]);
        }

        //check new mobile
        $user->mobile = $newMobile;
        $find = $user->find();
        if($find){
            if($user->uid == $uid){
                $this->j(0,'已经更新为新的手机号',['newMobile'=>$newMobile]);
            }else{
                $this->j(-3,'该手机号已经注册成另外一个账户!',['newMobile'=>$newMobile]);
            }
        }

        //update to new mobile
        $user->mobile = $newMobile;
        $user->save();
        $this->j(0,'已经更新为新的手机号',['newMobile'=>$newMobile]);
    }
	
	/**
	 * 用户信息
	 */
    public function infoAction()  {
        $uid = mylogin::getUid(true);
        $db = new mydb();
        $user = new User($db);
        $user->find(['uid', $uid]);
        if (!$user->uid)  {
        	//记录日志
            $log = new LoginLog();
            $log->userName = $uid;
            $log->ip = myrequest::getIP();
            require_once(LIB_PATH."ip/myip.php");
            $log->ipInfo = myip::getIpInfo();
            $log->reason = "非法登录";
            $log->created = date('Y-m-d H:i:s');
            $log->save();
            $this->j(-1, '用户不存在');
        }

        $needPay = NoteExt::getOrderCount($uid);
        $user->arr['needPay'] = $needPay;

        if(!preg_match("/^http/i",$user->arr['avatar'])){
            $user->arr['avatar']  = FRONT_URL.$user->arr['avatar'];
        }

        //学生
        $obj = new Student();
        $all = $obj->all(['uid',$uid]);
        $user->arr['student'] = $all;

        $this->j(0, '成功',  $user->tidy());
    }

    /**
     * nick
     */
    public function changeNickAction()
    {
        $uid = mylogin::getUid();
        $user = new User();
        $user->find(['uid', $uid]);
        if (!$user->uid)
        {
            $this->j(-1, '没有找到用户');
        }
        $user->nick = r('nick');
        if (verifyHelper::hasSpecialSymbols($user->nick))
        {
            $this->j(-1, '昵称不可以有特殊字符');
        }
        $user->save();
        $this->j(0, '昵称修改成功');
    }

}

